<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px" @submit.native.prevent>
      <el-form-item label="用户账号" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户账号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mall:shopUser:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mall:shopUser:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mall:shopUser:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mall:shopUser:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shopUserList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="账号ID" align="center" prop="suserId" />
      <!-- <el-table-column label="店铺ID" align="center" prop="shopId" /> -->
      <el-table-column label="用户账号" align="center" prop="userName" />
      <el-table-column label="用户昵称" align="center" prop="nickName" />
      <!-- <el-table-column label="用户类型 0 总账户 1 分帐户" align="center" prop="userType" /> -->
      <el-table-column label="用户邮箱" align="center" prop="email" width="300"/>
      <el-table-column label="手机号码" align="center" prop="phonenumber" width="110"/>
      <!-- <el-table-column label="用户性别" align="center" prop="sex" />
      <el-table-column label="头像地址" align="center" prop="avatar" /> -->
      <!-- <el-table-column label="密码" align="center" prop="password" /> -->
      <!-- <el-table-column label="帐号状态" align="center" prop="status" /> -->
      <el-table-column label="客服id" align="center" prop="serverId" />
      <!-- <el-table-column label="最后登陆IP" align="center" prop="loginIp" />
      <el-table-column label="最后登陆时间" align="center" prop="loginDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.loginDate) }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="用户状态" align="center" prop="disabled" :formatter="disabledFormate"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mall:shopUser:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mall:shopUser:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="handleRecommend(scope.row)"
            v-hasPermi="['mall:shopUser:edit']"
          >用户状态</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改店铺用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" >
        <el-form-item label="店铺ID" prop="shopId" v-if="false">
          <el-input  v-model="form.shopId"/>
        </el-form-item>
        <el-form-item label="用户账号" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户账号" />
        </el-form-item>
         <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" show-password/>
        </el-form-item>
        <el-form-item label="用户昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入用户昵称" />
        </el-form-item>
        <!-- <el-form-item label="用户类型 0 代表是总店 1 代表分店" prop="userType">
          <el-select v-model="form.userType" placeholder="请选择用户类型 0 代表是总店 1 代表分店">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item> -->
        <el-form-item label="用户邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入用户邮箱" />
        </el-form-item>
        <el-form-item label="手机号码" prop="phonenumber">
          <el-input v-model="form.phonenumber" placeholder="请输入手机号码" />
        </el-form-item>
        <!-- <el-form-item label="用户性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择用户性别">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="头像地址" prop="avatar">
          <el-input v-model="form.avatar" placeholder="请输入头像地址" />
        </el-form-item> -->

        <!-- <el-form-item label="帐号状态">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item> -->
        <!-- <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item> -->
        <el-form-item label="客服id" prop="serverId">
          <el-input v-model="form.serverId" placeholder="请输入客服id" />
        </el-form-item>
        <!-- <el-form-item label="最后登陆IP" prop="loginIp">
          <el-input v-model="form.loginIp" placeholder="请输入最后登陆IP" />
        </el-form-item>
        <el-form-item label="最后登陆时间" prop="loginDate">
          <el-date-picker clearable size="small"
            v-model="form.loginDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择最后登陆时间">
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="disabled" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户状态" prop="disabled" label-width="100">
          <el-radio-group v-model="form.disabled">
            <el-radio
              v-for="dict in disabledOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listShopUser, getShopUser, delShopUser, addShopUser, updateShopUser, exportShopUser } from "@/api/mall/shopUser";
export default {
  name: "ShopUser",
  components: {
  },
  data() {
    return {
      // 用户状态（0：正常 1：冻结）信息字典
      disabledOptions: [],
      // 用户状态弹出层
      disabled: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 店铺用户表格数据
      shopUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        shopId:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userName: [
          { required: true, message: "用户账号不能为空", trigger: "blur" }
        ],
        password: [
          { required: true, message: "用户密码不能为空", trigger: "blur" }
        ],
        nickName: [
          { required: true, message: "用户昵称不能为空", trigger: "blur" }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          {
            type: 'email',
            message: '请输入正确的邮箱地址',
            trigger: ['blur', 'change'],
          },
        ],
        phonenumber: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
           pattern: /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/,
            message: '请输入正确的手机号码或者座机号',
          },
        ],

      }
    };
  },
  created() {
    var shopId = this.$route.query.shopId;
    this.queryParams.shopId= shopId;
    this.getDicts("vx_memberUser_disabled").then(response => {
      this.disabledOptions = response.data;
    });
    this.getList();
  },
  methods: {
    /** 查询店铺用户列表 */
    getList() {
      //console.log(shopId);
      //this.queryParams.shopId= shopId;
      this.loading = true;
      listShopUser(this.queryParams).then(response => {
        this.shopUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 用户状态（0：正常 1：冻结）字典翻译
    disabledFormate(row, column) {
      return this.selectDictLabel(this.disabledOptions, row.disabled);
    },
    // 取消按钮
    cancel() {
      this.disabled = false;
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        disabled: null,
        suserId: null,
        shopId: null,
        userName: null,
        nickName: null,
        userType: null,
        email: null,
        phonenumber: null,
        sex: null,
        avatar: null,
        password: null,
        status: "0",
        delFlag: null,
        serverId: null,
        loginIp: null,
        loginDate: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.suserId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加店铺用户";
      this.form.shopId = this.queryParams.shopId
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const suserId = row.suserId || this.ids
      getShopUser(suserId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改店铺用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.suserId != null) {
            updateShopUser(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.disabled = false;
              this.open = false;
              this.getList();
            });
          } else {
            addShopUser(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.disabled = false;
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 用户状态按钮操作 */
    handleRecommend(row) {
      this.reset();
      const suserId = row.suserId || this.ids
      getShopUser(suserId).then(response => {
        this.form = response.data;
        this.disabled = true;
        this.title = "用户状态";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const suserIds = row.suserId || this.ids;
      this.$confirm('是否确认删除店铺用户编号为"' + suserIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delShopUser(suserIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有店铺用户数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportShopUser(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
